package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReviseBook implements IOPeration{
    @Override
    public void work(BookList bookList){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要修改的图书书号");
        String id0 = scan.next();
        //用于记录修改图书的索引位置
        int num = -1;
        for (int i = 0; i < bookList.books.size(); i++) {
            //检查是否有该书号的书
            Book Book = bookList.books.get(i);
            if (Book.id.equals(id0)) {
                num = i;
                System.out.println(Book);
            }
        }
        Book[] books = bookList.books.toArray(new Book[0]);
        if(num == -1){
            System.out.println("未找到该书号的书");
            return;
        }
        System.out.println("请输入修改后的余量:");
        int num0 = scan.nextInt();
        int Num = num0;
        for (int i = 0; i < books.length; i++) {
            Num += books[i].getNum();
        }
        Num -= books[num].getNum();
        if(Num > bookList.MAX_SIZE){
            System.out.println("超过书店图书容量,修改失败");
            return;
        }
        System.out.println("请输入修改后的作者:");
        String writer0 = scan.next();
        System.out.println("请输入修改后的类型:");
        String type0 = scan.next();
        Book book = books[num];
        book.setNum(num0);
        book.setType(type0);
        book.setWriter(writer0);
        bookList.books.set(num,book);
        System.out.println("修改成功");
    }
}
